const { queryUser } = require('./user.service')

// 处理用户信息
const handleUser = async (ctx, next) => {
  const user = ctx.request.body

  const phoneReg = /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/
  if (!phoneReg.test(user.phone)) {
    ctx.throw(500, "手机号不正确")
  }

  const idCardReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  if (!idCardReg.test(user.idCard)) {
    ctx.throw(500, "身份证不正确")
  }

  const result = await queryUser(user)
  if (result[0]) {
    ctx.throw(500, "该用户已注册")
  }

  await next()
}

module.exports = {
  handleUser
}
